package com.cmf.myproject.service.system.menu;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmf.myproject.dal.mybatis.mapper.SysMenuMapper;
import com.cmf.myproject.dal.mybatis.model.SysMenu;
import com.cmf.myproject.dal.mybatis.model.SysMenuExample;
import com.cmf.myproject.dal.mybatis.mymapper.MySysRoleResourceMapper;
import com.cmf.myproject.service.system.menu.dto.MenuNode;
import com.cmf.myproject.service.system.menu.dto.MenuNodeRsp;
import com.cmf.myproject.service.system.menu.dto.TreeGridDto;
import com.system.core.results.ResultBase;


@Service
public class MenuService {
	
	@Autowired
	private SysMenuMapper menuMapper;
	
	@Autowired
	private MySysRoleResourceMapper mySysRoleResourceMapper;
	
	public MenuNodeRsp editMenuNode(SysMenu sysMenu) {
		MenuNodeRsp result = new MenuNodeRsp();
		int update = menuMapper.updateByPrimaryKeySelective(sysMenu);
		if(update >0) {
			result.setSuccess(true);
			result.setData(sysMenu);
		} else {
			result.setMessage("更新菜单失败");
		}
		return result;
	}
	
	public MenuNodeRsp addMenuNode(SysMenu sysMenu) {
		MenuNodeRsp result = new MenuNodeRsp();
		int add = menuMapper.insertSelective(sysMenu);
		if(add >0) {
			result.setSuccess(true);
			result.setData(sysMenu);
		} else {
			result.setMessage("添加菜单失败");
		}
		return result;
	}
	
	public ResultBase delMenuNode(Long menuId) {
		ResultBase result = new ResultBase();
		SysMenuExample sysMenuExample = new SysMenuExample();
		sysMenuExample.createCriteria().andParentIdEqualTo(menuId);
		List<SysMenu> menus = menuMapper.selectByExample(sysMenuExample);
		if(menus.size() > 0) {
			result.setSuccess(false);
			result.setMessage("不能级联删除，请先删除下级菜单！");
			return result;
		}
		int delete = menuMapper.deleteByPrimaryKey(menuId);
		if(delete > 0) {
			result.setSuccess(true);
		}
		return result;
	}
	
	public TreeGridDto getTreeGrid() {
		TreeGridDto treeGridDto = new TreeGridDto();
		List<SysMenu> list = menuMapper.selectByExample(null);
		treeGridDto.setIs(true);
		treeGridDto.setData(list);
		treeGridDto.setCount(list.size());
		treeGridDto.setCode(0);
		treeGridDto.setTip("success");
		return treeGridDto;
	}

	public List<MenuNode> getMenusByParentId(long menuid,long roleId){
		List<MenuNode> rootnodes = new ArrayList<MenuNode>();
		SysMenuExample example = new SysMenuExample();
		example.createCriteria().andParentIdEqualTo(menuid);
		List<SysMenu> SysMenus = menuMapper.selectByExample(example);
		List<SysMenu> allMenu = menuMapper.selectByExample(null);
		
		List<SysMenu> menuResource = mySysRoleResourceMapper.findMenuByRoleId(roleId);
		List<Long> ids = new ArrayList<Long>();
		menuResource.forEach(leaf->{
			findMenuIdXPath(allMenu, leaf, ids);
		});
		if(roleId == -999) {
			allMenu.forEach(sysMenu -> {
				ids.add(sysMenu.getMenuId());
			});
		}
		ids.remove(new Long(0));
		
		for(SysMenu sysMenu:SysMenus){
			MenuNode rootnode = new MenuNode();
			rootnode.setHref(sysMenu.getMenuUrl());
			rootnode.setMenuIcon(sysMenu.getMenuIcon());
			rootnode.setMenuId(sysMenu.getMenuId());
			rootnode.setSpread(true);
			rootnode.setSort(sysMenu.getSort()==null?99999:sysMenu.getSort());
			rootnode.setTitle(sysMenu.getMenuName());
			if(ids.contains(sysMenu.getMenuId()))
				rootnodes.add(nodebuild(rootnode,allMenu,ids));
		}
		
		
		return rootnodes;
	}
	
	public void findMenuIdXPath(List<SysMenu> allMenu, SysMenu leaf, List<Long> ids) {
		if(!ids.contains(leaf.getParentId()))
		ids.add(leaf.getParentId());
		if(!ids.contains(leaf.getMenuId()))
		ids.add(leaf.getMenuId());
		
		if (leaf.getParentId() != 0) {
			for (SysMenu node : allMenu) {
				if (node.getMenuId() == leaf.getParentId())
					findMenuIdXPath(allMenu, node, ids);
			}
		}
	}
	
	private MenuNode nodebuild(MenuNode pnode,List<SysMenu> list,List<Long> allowIds){
		for(SysMenu nodedo:list){
			if(nodedo.getParentId()!=0 && nodedo.getParentId().equals(pnode.getMenuId()) && allowIds.contains(nodedo.getMenuId())){
				MenuNode node = new MenuNode();
				node.setHref(nodedo.getMenuUrl());
				node.setMenuIcon(nodedo.getMenuIcon());
				node.setMenuId(nodedo.getMenuId());
				node.setSpread(false);
				node.setTitle(nodedo.getMenuName());
				node.setSort(nodedo.getSort()==null?99999:nodedo.getSort());
				pnode.getChildren().add(node);
				nodebuild(node,list,allowIds);
			}
		}
		return pnode;
	}
}
